package com.xyc.gulimall.member.controller;

import com.xyc.common.exception.BizCodeEnume;
import com.xyc.common.utils.PageUtils;
import com.xyc.common.utils.R;
import com.xyc.gulimall.member.entity.MemberEntity;
import com.xyc.gulimall.member.exception.PhoneExistException;
import com.xyc.gulimall.member.exception.UsernameExistException;
import com.xyc.gulimall.member.feign.CouponFeignService;
import com.xyc.gulimall.member.service.MemberService;
import com.xyc.gulimall.member.vo.MemberLoginVo;
import com.xyc.gulimall.member.vo.MemberRegisterVo;
import com.xyc.gulimall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author xyc
 * @email xyc@gmail.com
 * @date 2024-07-15 19:14:00
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");

        R memberCoupons = couponFeignService.memberCoupons();

        return R.ok().put("member", memberEntity).put("coupons", memberCoupons.get("coupons"));
    }

    /**
     * 授权登录
     *
     * @param socialUser
     * @return
     * @throws Exception
     */
    @PostMapping("/oauth/login")
    public R oauthlogin(@RequestBody SocialUser socialUser) throws Exception {

        MemberEntity memberEntity = memberService.login(socialUser);
        if (memberEntity != null) {
            // TODO 1、登录成功处理
            return R.ok().put("data", memberEntity);
        } else {
            return R.error(
                    BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_INVAILD_EXCEPTION.getMsg()
            );
        }
    }

    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo memberRegisterVo) {
        System.out.println("开始注册");
        try {
            memberService.register(memberRegisterVo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnume.USER_EXIST_EXCEPTION.getCode(), BizCodeEnume.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo memberLoginVo) {

        MemberEntity memberEntity = memberService.login(memberLoginVo);
        if (memberEntity != null) {
            return R.ok().put("data",memberEntity);
        } else {
            return R.error(
                    BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_INVAILD_EXCEPTION.getCode(),
                    BizCodeEnume.LOGIN_ACCOUNT_PASSWORD_INVAILD_EXCEPTION.getMsg()
            );
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
